package cn.yunhe.servlet;

import cn.yunhe.dao.UserDao;
import cn.yunhe.model.PageBean;
import cn.yunhe.model.UserInfo;
import cn.yunhe.util.JsonUtil;
import cn.yunhe.util.ResponseUtil;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class UserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        UserDao userDao = new UserDao();

        JsonObject jsonObject = new JsonObject();


        String flag = request.getParameter("f");
        if (flag.equals("query")) {
            String page = request.getParameter("page");
            String rows = request.getParameter("rows");

            JSONObject result = new JSONObject();
            PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
            JSONArray jsonArray = JsonUtil.formatRsToJsonArray(userDao.userList(pageBean));
            //总条数
            int total = userDao.userCount();
            result.put("rows", jsonArray);
            result.put("total", total);

            ResponseUtil.write(response,result);

        } else if (flag.equals("add")) {
            UserInfo userinfo = new UserInfo();
            userinfo.setUsername(request.getParameter("username"));
            userinfo.setPassword(request.getParameter("password"));
            userinfo.setSex(request.getParameter("sex"));
            userinfo.setAge(request.getParameter("age"));
            userinfo.setXueli(request.getParameter("xueli"));
            userinfo.setAddress(request.getParameter("address"));

            if (userDao.addUser(userinfo) == 1) {
                jsonObject.addProperty("success", true);
                jsonObject.addProperty("message", "添加成功");
            } else {
                jsonObject.addProperty("success", false);
                jsonObject.addProperty("message", "添加失败");
            }

            out.write(jsonObject.toString());

        } else if (flag.equals("delete")) {
            int id = Integer.parseInt(request.getParameter("id"));
            if (userDao.deleteUser(id) == 1) {
                jsonObject.addProperty("success", true);
                jsonObject.addProperty("message", "删除成功");
            } else {
                jsonObject.addProperty("success", false);
                jsonObject.addProperty("message", "删除失败");
            }
            out.write(jsonObject.toString());

        } else if (flag.equals("update")) {
            UserInfo userinfo = new UserInfo();
            int id = Integer.parseInt(request.getParameter("id"));
            userinfo.setUsername(request.getParameter("username"));
            userinfo.setPassword(request.getParameter("password"));
            userinfo.setSex(request.getParameter("sex"));
            userinfo.setAge(request.getParameter("age"));
            userinfo.setXueli(request.getParameter("xueli"));
            userinfo.setAddress(request.getParameter("address"));

            if (userDao.updateUser(userinfo, id) == 1) {
                jsonObject.addProperty("success", true);
                jsonObject.addProperty("message", "修改成功");
            } else {
                jsonObject.addProperty("success", false);
                jsonObject.addProperty("message", "修改失败");
            }
            out.write(jsonObject.toString());
        }
        out.flush();
        out.close();

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
